package com.dlc.shop.systemtest.api.amount;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dlc.shop.bean.model.ShopWalletLog;
import com.dlc.shop.systemtest.constants.Constant;
import com.dlc.shop.systemtest.util.HttpUtil;

import java.util.List;
import java.util.Objects;

/**
 * @author xxw
 * @date 2023/2/20 11:35
 * 店铺交易记录API
 */
public class ShopWalletLogApi {

    /**
     * 根据订单编号查询交易记录
     * @param orderNumber
     * @return
     */
    public static List<ShopWalletLog> getShopWalletLogByOrderNumber(String orderNumber){
        String url = Constant.SHOP_URL + "/shop/shopWalletLog/page"+"?orderNumber="+ orderNumber;
        return execute(url, true);
    }

    /**
     * 根据订单编号和店铺id查询交易记录
     * @param orderNumber
     * @return
     */
    public static List<ShopWalletLog> getShopWalletLogByOrderNumberAndShopId(String orderNumber, Long shopId){
        String url = Constant.SHOP_URL + "/shop/shopWalletLog/page"+"?orderNumber="+ orderNumber + "&shopId=" + shopId;;
        return execute(url, Objects.equals(shopId, 1) ? true : false);
    }


    /**
     * 根据退款编号查询交易记录
     * @param refundSn
     * @return
     */
    public static List<ShopWalletLog> getShopWalletLogByRefundSn(String refundSn){
        String url = Constant.SHOP_URL + "/shop/shopWalletLog/page"+"?refundSn="+ refundSn;
        return execute(url, true);
    }

    /**
     * 根据退款编号和店铺id查询交易记录
     * @param refundSn
     * @return
     */
    public static List<ShopWalletLog> getShopWalletLogByRefundSnAndShopId(String refundSn, Long shopId){
        String url = Constant.SHOP_URL + "/shop/shopWalletLog/page"+"?refundSn="+ refundSn + "&shopId=" + shopId;
        return execute(url, Objects.equals(shopId, 1) ? true : false);
    }

    /**
     * 获取提现产生的记录
     * @param reason
     * @param changeAomunt
     * @return
     */
    public static List<ShopWalletLog> getWithdrawShopWalletLog(Integer reason, Double changeAomunt){
        String url = Constant.SHOP_URL + "/shop/shopWalletLog/page"+"?reason="+ reason + "& changeAomunt =" +changeAomunt +"&" ;
        return execute(url, true);
    }

    private static List<ShopWalletLog> execute(String url, Boolean useMainShop){
        Object page = HttpUtil.get(url, Object.class, useMainShop);
        JSONObject jsonObject = JSON.parseObject(page.toString());
        Object records = jsonObject.get("records");
        List<ShopWalletLog> shopWalletLogs = JSON.parseArray(records.toString(), ShopWalletLog.class);
        return shopWalletLogs;
    }

}
